<?php

/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2017/6/28
 * Time: 14:38
 */

namespace Admin\Service;

use Service\BaseService;

class CountService extends BaseService {



    public function __construct() {
        parent::__construct();
        $this->countModel = M('question');
    }

    public function searchList($title,$username,$mobile,$isPage = 1, $nowPage = 1) {
        if ($title) {
            $where['sh.title'] = array('like', '%'.$title.'%');
        }
        if ($username) {
            $where['u.username'] = array('like', '%'.$username.'%');
        }
        if ($mobile) {
            $where['u.mobile'] = $mobile;
        }

        $obj = M('search_history')->alias('sh')->field('sh.*,u.username,u.mobile')
            ->join(' LEFT JOIN ' . C('DB_PREFIX') . 'users AS u ON u.user_id = sh.user_id')
             ->order('id desc');

        if ($where) {
            $obj->where($where);
        }
        if ($isPage) {
            $countRes = $obj->count();
            self::bPage($countRes);
            return self::resReturn(200, "success", self::$pageObj);
        } else {
            $lineList = $obj->page($nowPage, self::$pageNum)->select();
            return self::resReturn(200, 'success', $lineList);
        }
    }


    public function handleCount($param) {

        $act = $param['act'];

        switch ($act) {

            case 'del':
                $res = $this->_deleteConduct($param['id']);
                return $res;
                break;
            default:
                break;
        }

    }

    private function _deleteConduct($lineId = 0) {

        if (!$lineId) {

            return self::resReturn(130, '删除失败');
        }

        $delRes = M('search_history')->where('id = '.$lineId)->delete();
        if (!$delRes) {

            return self::resReturn(131, '删除失败');
        }

        return self::resReturn(200, '删除成功');
    }



}
